﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using Plumber.Accounting.ModuleData;
using Plumber.Masters.ModuleData;

namespace Plumber.Accounting.Preferences
{
    public partial class AccountingPreferencePanel : AccountingPreferencePanelNoVis
    {
        public AccountingPreferencePanel()
            : base()
        {
            InitializeComponent();
        }

        public AccountingPreferencePanel(string appName)
            : base(appName)
        {
            InitializeComponent();
            ButtonText = Properties.Resources.T_Accounting;
            ButtonImage = Properties.Resources.B_Accounting;
        }

        protected override void OnBindData()
        {
            BindControl(ckbEnable, "EnableLink", "Checked");
            BindControl(ckbLockJE, "LockDocumentFromSale", "Checked");
            BindControl(cbbLockJEPayed, "LockDocumentPayed", "Checked");
            
            BindControl(ckbEnableTax, "EnableTax", "Checked");
            BindControl(ctbDateTax, "DateTax", "Today");
            BindControl(cbbSale, "SaleTax", "SelectedValue");
            BindControl(cbbPurchase, "PurchaseTax", "SelectedValue");
            BindControl(cbbTypeTreasury, "TypeTreasuryTax", "SelectedValue");
            BindControl(rdrMasterTreasury, "MasterTreasuryTax");

            BindControl(cbbInvoice,  "Invoice", "SelectedValue");
            BindControl(cbbInvoiceDeduction, "InvoiceDeduction", "SelectedValue");
            BindControl(cbbInvoiceRestraint, "InvoiceRestraint", "SelectedValue");

            BindControl(cbbCreditNote,  "CreditNote", "SelectedValue");
            BindControl(cbbCreditNoteDeduction, "CreditNoteDeduction", "SelectedValue");
            BindControl(cbbCreditNoteRestraint, "CreditNoteRestraint", "SelectedValue");

            BindControl(cbbDebitNote,  "DebitNote", "SelectedValue");
            BindControl(cbbDebitNoteDeduction, "DebitNoteDeduction", "SelectedValue");
            BindControl(cbbDebitNoteRestraint, "DebitNoteRestraint", "SelectedValue");

            BindControl(cbbBankRestraint, "BankRestraint", "SelectedValue");
            BindControl(rdrBankRestraint, "BankRestraintCode");
        }

        protected override void OnBeforeEdit()
        {
        }

        protected override void OnPrepareAuxData()
        {
        }

        protected override bool OnBeforeSave()
        {
            if (ctbDateTax.Today == null)
                ctbDateTax.Today = DateTime.Today;

            return true;
        }

        protected override void OnDisableControlsForEdit()
        {
            ckbLockJE.Enabled = cbbPurchase.Enabled = cbbSale.Enabled = cbbTypeTreasury.Enabled = ctbDateTax.Enabled = ckbEnableTax.Checked;
        }

        protected override void OnAttachData()
        {
            cbbSale.AttachDataReader<string>(new RRMasterTypeForTax(), PL_TaxCode.Code, PL_TaxCode.Description);
            cbbPurchase.AttachDataReader<string>(new RRMasterTypeForTax(), PL_TaxCode.Code, PL_TaxCode.Description);
            cbbTypeTreasury.AttachDataReader<string>(new RRMasterTypeForAccountingMaster(), PL_TaxCode.Code, PL_TaxCode.Description);
            cbbInvoice.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbInvoiceDeduction.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbInvoiceRestraint.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbCreditNote.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbCreditNoteDeduction.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbCreditNoteRestraint.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbDebitNote.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbDebitNoteDeduction.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            cbbDebitNoteRestraint.AttachDataReader<string>(new RRJETemplate(true), PL_JETemplate.Template, PL_JETemplate.Description);
            rdrMasterTreasury.RadarForm = new RadarMasterForm("",RadarMasterForm.Module.Accounting);
            rdrBankRestraint.RadarForm = new RadarMasterForm("", RadarMasterForm.Module.Accounting);

            cbbBankRestraint.AttachDataReader<string>(new RRMasterTypeForAccountingMaster(), PL_TaxCode.Code, PL_TaxCode.Description);
        }

        private void ckbEnableTax_CheckStateChanged(object sender, EventArgs e)
        {
            if (DocumentMode == DBMode.Browse)
                return;

            cbbPurchase.Enabled = cbbSale.Enabled = cbbTypeTreasury.Enabled = ctbDateTax.Enabled = ckbEnableTax.Checked;
        }

        private void cbbTypeTreasury_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((RadarMasterForm)rdrMasterTreasury.RadarForm).FilterType = cbbTypeTreasury.GetValue<string>();
        }

        private void cbbBankRestraint_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((RadarMasterForm)rdrBankRestraint.RadarForm).FilterType = cbbBankRestraint.GetValue<string>();
        }
    }

    #region AccountingPreferencePanelNoVis

    public partial class AccountingPreferencePanelNoVis : ApplicationLibrary.Preferences.GenericPreferencePanel<AccountingPref>
    {
        public AccountingPreferencePanelNoVis()
        { }

        public AccountingPreferencePanelNoVis(string appName)
            : base(appName, null)
        {
        }
    }

    #endregion

    [DataContract]
    public class AccountingPref
    {
        [DataMember]
        public bool EnableLink { get; set; }

        [DataMember]
        public bool EnableTax { get; set; }

        [DataMember]
        public string MasterTax { get; set; }

        [DataMember]
        public string SaleTax { get; set; }

        [DataMember]
        public string PurchaseTax { get; set; }

        [DataMember]
        public string TypeTreasuryTax { get; set; }

        [DataMember]
        public string MasterTreasuryTax { get; set; }

        [DataMember]
        public DateTime DateTax { get; set; }

        [DataMember]
        public string Invoice { get; set; }

        [DataMember]
        public string InvoiceDeduction { get; set; }
        
        [DataMember]
        public string InvoiceRestraint { get; set; }

        [DataMember]
        public string CreditNote { get; set; }

        [DataMember]
        public string CreditNoteDeduction { get; set; }

        [DataMember]
        public string CreditNoteRestraint { get; set; }

        [DataMember]
        public string DebitNote { get; set; }

        [DataMember]
        public string DebitNoteDeduction { get; set; }

        [DataMember]
        public string DebitNoteRestraint { get; set; }

        [DataMember]
        [DefaultValue(true)]
        public bool LockDocumentFromSale { get; set; }

        [DataMember]
        [DefaultValue(true)]
        public bool LockDocumentPayed { get; set; }

        [DataMember]
        public string BankRestraint { get; set; }

        [DataMember]
        public string BankRestraintCode { get; set; }

        public AccountingPref()
        {
        }

        //
        public bool IsTaxEnabled(DateTime date)
        {
            return EnableTax && date >= DateTax;
        }
    }
}